package edu.nf.project.webs.employeeAndCustomerInformation;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.Member;
import edu.nf.project.services.exception.derivedAnomalies.MemberException;
import edu.nf.project.services.employeeAndCustomerInformation.MemberService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会员控制器
 */
@RestController
@RequestMapping("/api/members")
public class MemberController extends BaseController {
    @Autowired
    private MemberService memberService;

    /**
     * 获取会员列表
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 会员列表
     */
    @GetMapping
    public ResultVO<Page<Member>> getMemberList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<Member> page = new Page<>(pageNum, pageSize);
            Page<Member> result = memberService.getMemberList(page);
            return success(result);
        } catch (Exception e) {
            if (e instanceof MemberException) {
                throw e;
            }
            throw new MemberException(50000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取会员详细信息
     * @param memberId 会员ID
     * @return 会员信息
     */
    @GetMapping("/{memberId}")
    public ResultVO<Member> getMemberInfo(@PathVariable Integer memberId) {
        try {
            Member member = memberService.getMemberInfo(memberId);
            return success(member);
        } catch (Exception e) {
            if (e instanceof MemberException) {
                throw e;
            }
            throw new MemberException(50000, "系统错误，请稍后重试");
        }
    }

    /**
     * 更新用户会员等级
     * @param customerId 用户ID
     * @param memberId 会员等级ID
     * @return 是否更新成功
     */
    @PutMapping("/customers/{customerId}")
    public ResultVO<Boolean> updateCustomerMemberLevel(
            @PathVariable Integer customerId,
            @RequestParam Integer memberId) {
        try {
            boolean result = memberService.updateCustomerMemberLevel(customerId, memberId);
            return success(result);
        } catch (Exception e) {
            if (e instanceof MemberException) {
                throw e;
            }
            System.out.println(e);
            throw new MemberException(50000, "系统错误，请稍后重试");
        }
    }
} 